package com.sunrise.visual.frame.component
{
	import com.degrafa.Graphic;
	
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.SpreadMethod;
	import flash.geom.Matrix;
	import flash.text.TextLineMetrics;
	
	import mx.containers.HBox;
	import mx.controls.Label;
	import mx.controls.ProgressBar;
	import mx.core.UIComponent;
	
	[Style(name="defaultColor", type="uint", format="Color", inherit="no")]
	[Style(name="defaultAlpha", type="Number", format="Length", inherit="no")]
	[Style(name="processColor", type="uint", format="Color", inherit="no")]
	[Style(name="processAlpha", type="Number", format="Length", inherit="no")]
	public class ProcessBar extends HBox
	{
		public function ProcessBar()
		{
			super();
			this.setStyle("verticalAlign","middle");
			this.setStyle("paddingLeft",5);
			this.setStyle("paddingRight",5);
		}
		
		private var _minimum:Number = 0;
		public function get minimum():Number
		{
			return _minimum;
		}
		
		public function set minimum(value:Number):void
		{
			if (!isNaN(value) && value != _minimum)
			{
				_minimum = value;
				invalidateDisplayList();
			}
		}
		
		private var _maximum:Number = 0;
		public function get maximum():Number
		{
			return _maximum;
		}

		public function set maximum(value:Number):void
		{
			if (!isNaN(value) && value != _maximum)
			{
				_maximum = value;
				invalidateDisplayList();
			}
		}
		
		private var _value:Number = 0;
		
		public function get value():Number
		{
			return _value;
		}
		
		private var uic:UIComponent;
		private var lbl:Label;
		override protected function createChildren():void {
			super.createChildren();
			if(uic == null)
				uic = new UIComponent();
			uic.percentHeight = 80;
			uic.percentWidth = 100;
			this.addChild(uic);
			if(lbl == null) 
				lbl = new Label();
			lbl.setStyle("color", 0xffffff);
			this.addChild(lbl);
		}
		
		override protected function updateDisplayList(width:Number, height:Number):void {
			super.updateDisplayList(width, height);
			lbl.text = _value +"";
			var w:int = uic.width;
			var h:int = uic.height;
			var x1:int = 0;
			var w1:int = _maximum == 0? 0: (w * (_value /_maximum));
			var x2:int = w1;
			var w2:int = w - w1;
			var y:int = 0;
			uic.graphics.clear();
			var matr:Matrix = new Matrix();
			matr.createGradientBox(5, h, 0, 0, 0);
			uic.graphics.beginGradientFill(GradientType.LINEAR,
				[this.getStyle("processColor")], [this.getStyle("processAlpha")], [0x00], matr, SpreadMethod.REPEAT);
			uic.graphics.drawRect(x1, y, w1, h);
			var matr1:Matrix = new Matrix();
			matr1.createGradientBox(5, h, 0, 0, 0);
			uic.graphics.beginGradientFill(GradientType.LINEAR, 
				[this.getStyle("defaultColor")], [this.getStyle("defaultAlpha")], [0x00], matr1, SpreadMethod.REPEAT);
			uic.graphics.drawRect(x2, y, w2, h);
			
			var g:Graphics = uic.graphics;
			g.lineStyle(1,0x1a1a1a,1);		
			var startX:Number = x1;
			var startY:Number = 0;
			
			g.moveTo( startX, startY );
			g.lineTo( w, startY );
			g.lineTo( w, h );	
			g.lineTo( startX, h );	
			g.lineTo( startX, startY );	
			g.endFill();
		}
		
		public function setProgress(value:Number, maximum:Number):void
		{
			if (!isNaN(value) && !isNaN(maximum))
			{
				_value = value;
				_maximum = maximum;
				invalidateDisplayList();
				this.invalidateProperties();
			}
		}
		
		override protected function commitProperties():void {
			super.commitProperties();
			lbl.text = _value + "";
			var tlm:TextLineMetrics = lbl.measureText("11.11");
			lbl.width = tlm.width + 5;
			lbl.height = tlm.height ;
		}
	}
}